package com.javaweb.demo.servlet;

import com.javaweb.demo.dao.UserDao;
import com.javaweb.demo.dao.UserDaolmpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AddMessageServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String delivername = request.getParameter("username");
        String receivername = request.getParameter("receivername");
        String string = request.getParameter("string");
        String username = request.getParameter("username");
        request.setAttribute("username",username);
        UserDao ud = new UserDaolmpl();
        if(ud.addmessage(delivername,receivername,string)) {
            request.setAttribute("xiaoxi", "添加成功");
            if (username.equals("daikunqi")) {
                request.getRequestDispatcher("/successM.jsp").forward(request,response);//转发
            } else {
                request.getRequestDispatcher("/success.jsp").forward(request,response);//转发
            }
        } else {
            request.getRequestDispatcher("/fail.jsp").forward(request, response);
        }
    }

}
